package com.xie.controller;

import com.xie.pojo.Homework;
import com.xie.service.HomeworkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.sql.Date;
import java.util.List;

@Controller
@RequestMapping("/homework")
public class HomeworkController {

    @Autowired
    @Qualifier("HomeworkServiceImpl")
    private HomeworkService homeworkService;

    @RequestMapping("/allHomework")
    public String allHomework(HttpSession session){
        List<Homework> homework = homeworkService.queryAllHomework();
        session.setAttribute("homeworkList",homework);
        return "homework";
    }

    /**
     *  只有第一次从welomes网页进有效，这个bug先不修了，有空再修2021.6.21
     * @param session
     * @param sname
     * @return
     */
    @RequestMapping("/allHomeworks")
    public String allHomeworks(HttpSession session,@RequestParam("sname") String sname){
        List<Homework> homework = homeworkService.queryHomeworkByName(sname);
        session.setAttribute("homeworkList",homework);
        return "homeworks";
    }

    @RequestMapping("/deleteHomework")
    public String deleteHomework(int homework_id, Model model){
        int i = homeworkService.deleteHomework(homework_id);
        if(i > 0){
            return "redirect:/homework/allHomework";
        }else{
            model.addAttribute("msg","删除失败");
            return "redirect:/homework/allHomework";
        }
    }
    @RequestMapping("/deleteHomeworks")
    public String deleteHomeworks(int homework_id,String sname, Model model,HttpSession session){
        int i = homeworkService.deleteHomework(homework_id);
        if(i > 0){
            List<Homework> homework = homeworkService.queryHomeworkByName(sname);
            session.setAttribute("homeworkList",homework);
            return "homeworks";
        }else{
            model.addAttribute("msg","删除失败");
            return "homeworks";
        }
    }

    @RequestMapping("/toAddHomework")
    public String toAddHomework(){
        return "addHomework";
    }
    @RequestMapping("/addHomework")
    public String addHomework(Homework homework,Model model){
        homework.setSub_time(new Date(System.currentTimeMillis()));
        int i = homeworkService.addHomework(homework);
        if (i > 0){
            return "redirect:/homework/allHomework";
        }else{
            model.addAttribute("msg","添加失败");
            return "redirect:/homework/toAddHomework";
        }
    }
    @RequestMapping("/toAddHomeworks")
    public String toAddHomeworks(){
        return "addHomeworks";
    }
    @RequestMapping("/addHomeworks")
    public String addHomeworks(Homework homework,Model model,HttpSession session){
        homework.setSub_time(new Date(System.currentTimeMillis()));
        int i = homeworkService.addHomework(homework);
        if (i > 0){

            List<Homework> homeworkss = homeworkService.queryHomeworkByName(homework.getSname());
            session.setAttribute("homeworkList",homeworkss);
            return "homeworks";
        }else{
            model.addAttribute("msg","添加失败");
            return "homeworks";
        }
    }

    @RequestMapping("/toUpdateHomework")
    public String toUpdateHomework(int homework_id,Model model){
        Homework homework = homeworkService.queryHomework(homework_id);
        model.addAttribute("QHomework",homework);
        return "updateHomework";
    }
    @RequestMapping("/updateHomework")
    public String updateHomework(Homework homework,Model model){
        int i = homeworkService.updateHomework(homework);
        if (i > 0){
            return "redirect:/homework/allHomework";
        }else{
            model.addAttribute("msg","添加失败");
            return "redirect:/homework/allHomework";
        }

    }







}